package com.vote.admin.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.vote.admin.service.AdminRecordService;
import com.vote.admin.service.AdminVoteService;
import com.vote.admin.vo.param.VoteParam;
import com.vote.admin.vo.result.RecordResult;
import com.vote.admin.vo.result.VoteResult;
import com.vote.common.entity.VoteOption;
import com.vote.common.entity.VoteProject;
import com.vote.common.returns.RespCode;
import com.vote.common.returns.RespEntity;
import com.vote.common.returns.RespTable;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin-vote")
@Slf4j
@Api(value = "管理平台投票管理", tags = "管理平台投票管理")
public class AdminVoteController {

    @Resource
    private AdminVoteService adminVoteService;

    @GetMapping("/list")
    @ApiOperation(value = "投票活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "column", value = "字段名", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "limit", value = "字段值", required = false)
    })
    public RespTable<List<VoteResult>> userList(@RequestParam(value = "page", required = true) Integer page,
                                                @RequestParam(value = "limit", required = true) Integer limit,
                                                @RequestParam(value = "column", required = false) String column,
                                                @RequestParam(value = "value", required = false) String value,
                                                HttpSession session) {
        RespTable<List<VoteResult>> respTable = adminVoteService.voteList(page, limit, column, value, session);
        return respTable;
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传活动封面")
    public RespEntity upload(MultipartFile file){
        RespEntity respEntity = adminVoteService.upload(file);
        return respEntity;
    }

    @PostMapping("/insert")
    @ApiOperation(value = "发布投票活动")
    public RespEntity publishVote(VoteProject voteProject, HttpSession session) {
        RespEntity respEntity = adminVoteService.publishVote(voteProject, session);
        return respEntity;
    }

    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出Excel")
    public void exportExcelAll(HttpServletResponse response, HttpSession session) {
        adminVoteService.exportExcelAll(response, session);
    }

    @PutMapping("/update")
    @ApiOperation(value = "编辑投票活动")
    public RespEntity updateVote(VoteParam voteParam) {
        RespEntity respEntity = adminVoteService.updateVote(voteParam);
        return respEntity;
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除投票活动")
    @ApiImplicitParam(paramType = "delete", dataType = "Integer", name = "id", value = "活动id", required = true)
    public RespEntity deleteVote(@RequestParam(value = "id") Integer id) {
        RespEntity respEntity = adminVoteService.deleteVote(id);
        return respEntity;
    }

    @GetMapping("/option-list")
    @ApiOperation(value = "选项列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "content", value = "搜索内容", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "活动id", required = true)
    })
    public RespTable<List<VoteOption>> optionList(@RequestParam(value = "page", required = true) Integer page,
                                                  @RequestParam(value = "limit", required = true) Integer limit,
                                                  @RequestParam(value = "content", required = false) String content,
                                                  @RequestParam(value = "id", required = true) Integer id) {
        RespTable<List<VoteOption>> respTable = adminVoteService.optionList(page, limit, content, id);
        return respTable;
    }

    @PostMapping("/option-upload")
    @ApiOperation(value = "上传选项图片")
    public RespEntity optionUpload(MultipartFile file){
        RespEntity respEntity = adminVoteService.optionUpload(file);
        return respEntity;
    }

    @PostMapping("/insert-option")
    @ApiOperation(value = "新增选项")
    public RespEntity insertOption(VoteOption voteOption) {
        RespEntity respEntity = adminVoteService.insertOption(voteOption);
        return respEntity;
    }

    @PutMapping("/update-option")
    @ApiOperation(value = "编辑选项")
    public RespEntity updateOption(VoteOption voteOption) {
        RespEntity respEntity = adminVoteService.updateOption(voteOption);
        return respEntity;
    }

    @DeleteMapping("/delete-option")
    @ApiOperation(value = "删除选项")
    @ApiImplicitParam(paramType = "delete", dataType = "Integer", name = "id", value = "选项id", required = true)
    public RespEntity deleteOption (@RequestParam(value = "id") Integer id) {
        RespEntity respEntity = adminVoteService.deleteOption(id);
        return respEntity;
    }
}
